﻿using System;
using System.Collections.Generic;
using System.Drawing;
using Simple3DRedactor.Domain;

namespace SceneRedactor
{
    class LightView : ObjectView<Light>
    {
        private List<Color> availableAmbientColors = new List<Color> { Color.Black, Color.White, Color.Green, Color.Red };
        private List<Color> availableDiffuseColors = new List<Color> { Color.Black, Color.White, Color.Green, Color.Red };
        private List<Color> availableSpecularColors = new List<Color> { Color.Black, Color.White, Color.Green, Color.Red };

        public LightView(Light obj) : base(obj)
        {
        }

        public override void ShowProperties(SimpleSceneRedactor redactor)
        {
            base.ShowProperties(redactor);

            redactor.ambientComboBox.DataSource = availableAmbientColors;
            redactor.ambientComboBox.SelectedIndex = availableAmbientColors.FindIndex(c => c == InnerObject.Ambient);
            redactor.diffuseColorComboBox.DataSource = availableDiffuseColors;
            redactor.diffuseColorComboBox.SelectedIndex = availableDiffuseColors.FindIndex(c => c == InnerObject.Diffuse);
            redactor.specularComboBox.DataSource = availableSpecularColors;
            redactor.specularComboBox.SelectedIndex = availableSpecularColors.FindIndex(c => c == InnerObject.Specular);

            redactor.constantAttenuationTextBox.Text = InnerObject.ConstantAttenuation.ToString();
            redactor.linearAttenuationTextBox.Text = InnerObject.LinearAttenuation.ToString();
            redactor.quadraticAttenuationTextBox.Text = InnerObject.QuadraticAttenuation.ToString();
            redactor.spotCutOffTextBox.Text = InnerObject.SpotCutOff.ToString();

            redactor.lightPropertiesGroupBox.Visible = true;
            redactor.lightPropertiesGroupBox.Location = new Point(redactor.specularLabel.Location.X, redactor.specularLabel.Location.Y + 20);
        }

        public override void SaveProperties(SimpleSceneRedactor redactor)
        {
            base.SaveProperties(redactor);

            if (redactor.ambientComboBox.SelectedIndex > -1)
                InnerObject.Ambient = availableAmbientColors[redactor.ambientComboBox.SelectedIndex];

            if (redactor.diffuseColorComboBox.SelectedIndex > -1)
                InnerObject.Diffuse = availableDiffuseColors[redactor.diffuseColorComboBox.SelectedIndex];

            if (redactor.specularComboBox.SelectedIndex > -1)
                InnerObject.Specular = availableSpecularColors[redactor.specularComboBox.SelectedIndex];
            try
            {
                InnerObject.ConstantAttenuation = float.Parse(redactor.constantAttenuationTextBox.Text);
                InnerObject.LinearAttenuation = float.Parse(redactor.linearAttenuationTextBox.Text);
                InnerObject.QuadraticAttenuation = float.Parse(redactor.quadraticAttenuationTextBox.Text);
                InnerObject.SpotCutOff = float.Parse(redactor.spotCutOffTextBox.Text);
            }
            catch(Exception)
            {
            }
        }

        public override string ToString()
        {
            return string.Format("{0} [Light]", InnerObject.Name);
        }
    }
}
